﻿@rendermode InteractiveServer

@if (!string.IsNullOrEmpty(DisplayMessage))
{
	var statusMessageClass = DisplayMessage.StartsWith("Error") ? "danger" : "success";
	<div class="alert alert-@statusMessageClass" role="alert">
		@DisplayMessage
	</div>
}

@inject AuthenticationStateProvider AuthProvider
@code {
	private string? messageFromCookie;

	[Parameter]
	public string? Message { get; set; }


	private AuthenticationState authState;
	// [CascadingParameter]
	// private HttpContext HttpContext { get; set; } = default!;

	private string? DisplayMessage => Message ?? messageFromCookie;

	protected override async Task OnInitializedAsync()
	{
		// messageFromCookie = HttpContext.Request.Cookies[IdentityRedirectManager.StatusCookieName];

		// if (messageFromCookie is not null)
		// {
		// 	HttpContext.Response.Cookies.Delete(IdentityRedirectManager.StatusCookieName);
		// }
	}
	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			try
			{
				authState = await AuthProvider.GetAuthenticationStateAsync();
				var user = authState.User;
				messageFromCookie = user.Identity?.Name;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
			}
		}
	}
}
